package com.xh.bussiness.xkw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.xkw.dao.RelationShipMapper;
import com.xh.bussiness.xkw.model.RelationShip;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * @author tw
 * @description 用户关联关系操作类
 */
@Service("relationShipService")
public class RelationShipService  extends BaseServiceImpl<RelationShipMapper,RelationShip> {


	/**
	 * @author: tw
	 * @description: 添加openId与用户的关联映射关系
	 * @param: [userId, openId, addTime]
	 * @return: boolean
	 */
	public boolean addUserRelationShip(String userId,String openId,Date addTime) throws Exception {
		RelationShip relationShip=new RelationShip();
		relationShip.setUserId(userId);
		relationShip.setOpenId(openId);
		relationShip.setAddTime(addTime);
		boolean save = insert(relationShip);
		return save;
	}
	
	/**
	 * @author: tw
	 * @description: 根据userId 查询openId与用户的关联映射关系是否已经绑定
	 * @param: [userId]
	 * @return: com.xh.xkw.pojo.RelationShip
	 */
	public RelationShip getByUserId(String userId) throws Exception {
		QueryWrapper<RelationShip> qw = new QueryWrapper<>();
		qw.eq("user_id",userId);
		return this.dao.selectOne(qw);
	}

	/**
	 * @author: tw
	 * @description: 维护openId与userId的绑定关系;在存储对应关系时，第三方应用需判断当前用户是否已经绑定openId，如果已经绑定则进行更新，如果未绑定则进行插入；
	 * @param: [userId, openId, addTime]
	 * @return: boolean
	 */
	public boolean updateUserRelationShip(String userId, String openId, Date addTime) throws Exception {
		RelationShip relationShip = getByUserId(userId);
		if(null != relationShip){
			relationShip.setOpenId(openId);
			relationShip.setAddTime(addTime);
			update(relationShip);
			return true;
		}
		return false;
	}
}
